package com.bph.user.controller;

import com.bph.commons.pojo.User;
import com.bph.commons.vo.ResultVo;
import com.bph.user.service.MailService;
import com.bph.user.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {
//    @HystrixCommand(fallbackMethod = "fallBack",commandProperties = {
//            @HystrixProperty(name="circuitBreaker.enabled",value="true"),//开启熔断器
//            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value="10000"),//时间
//            @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value="10"),//次数
//            @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value="50")//服务错误率
//    })
//    public String testHystrix(@RequestParam("num")Integer num){
//        int n = (int)Math.floor(Math.random()*100);
//        int result = n/num;
//        return "服务正常返回: "+n+"/"+num+"="+result;
//    }
    @Resource
    private UserService userService;
    @Resource
    private MailService mailService;

    /***
     *根据邮箱、密码登录
     */
    @PostMapping("/selectUserByPassword")
    public ResultVo selectUserByPassword(User user){
        return userService.selectUserByPassword(user);
    }

    /***
     *发送验证码到指定邮箱
     */
    @PostMapping("/getCodeByMail")
    public ResultVo getCodeByMail( User user){
        return mailService.getCodeByMail(user);
    }

    /***
     *根据邮箱、验证码登录
     */
    @PostMapping("/selectUserByCode")
    public ResultVo selectUserByCode(User user, String code, HttpServletRequest request){
         return userService.selectUserByCode(user, code, request);
    }

    /***
     *忘记密码
     */
    @PostMapping("/updatePasswordByCode")
    public  ResultVo updatePasswordByCode(User user, String code, HttpServletRequest request){
        System.out.println(code+" "+user.getUser_password());
        return userService.updatePasswordByCode(user,code,request);
    }

    /**
     *insertUserByMail  注册店长
     * 考虑到文档中该方法为完善，传入的user对象里有数据就填入对应数据
     * user对象中没有数据的字段就填入null，还要接收初始验证码，然后通过接收cookie来获取用户输入的验证码
     */
    @PostMapping("/insertUserByCode")
    public  ResultVo insertUserByCode(User user, String code, HttpServletRequest request){
        return userService.insertUserByMail(user,code,request);
    }

    /**
     * 获取用户权限
     *
     */
    @PostMapping("/getPermissionByMail")
    public  ResultVo getPermissionByMail(HttpServletRequest request){
        return userService.getPermissionByMail(request);
    }

    /**
     * 修改个人密码
     * @param user
     * @return
     */
    @PostMapping("/updatePasswordByPassword")
    public  ResultVo updatePasswordByPassword(User user, String new_password){
        System.out.println(user.getUser_mail()+" "+user.getUser_password()+" "+new_password);
        return userService.updatePasswordByPassword(user,new_password);
    }

    /**
     * 获得用户信息
     */
    @PostMapping("/getUserByMail")
    public  ResultVo getUserByMail(HttpServletRequest request){
        return userService.getUserByMail(request);
    }

    /**
     * 修改个人信息
     */
    @PostMapping("/updateUserByMail")
    public  ResultVo updateUserByMail(User user){

        System.out.println(user);
        return userService.updateUserByMail(user);
    }

    /**
     * 获取员工列表
     */
    @PostMapping("/getUserById")
    public ResultVo getUserById(@RequestBody User user){
        return userService.getUserById(user);
    }

    /**
     *根据id或姓名查询店员
     */
    @PostMapping("selectUserByKey")
    public  ResultVo selectUserByKey(String key, String user_title, String store_id, String limit_index, String limit_length){
        System.out.println(key+" "+user_title+" "+store_id+" "+limit_index+" "+limit_length);
        int storeId = Integer.parseInt(store_id);
        int limitIndex = Integer.parseInt(limit_index);
        int limitLength = Integer.parseInt(limit_length);
        return userService.selectUserByKey(key,user_title,storeId,limitIndex,limitLength);
    }

    /**
     * 根据userid删除用户，注意外键
     * @param user
     * @return
     */
    @PostMapping("deleteUserById")
    public ResultVo deleteUserById(User user){
        System.out.println(user.getUser_id());
        return userService.deleteUserById(user.getUser_id());
    }

    /**
     * 修改用户 修改user表内容 普通修改，不能修改重要词条
     * @param user
     * @return
     */
    @PostMapping("updateUserById")
    public ResultVo updateUserById(User user){
        System.out.println(user.getUser_name());
        return userService.updateUserById(user);
    }

    /**
     * 无偏好添加普通员工
     * @param user
     * @return
     */
    @PostMapping("insertEmployeeByMail")
    public ResultVo insertEmployeeByMail(User user){
        System.out.println(user.getUser_mail());
        return userService.insertEmployeeByMail(user);
    }
}
